package com.oracle.oa.action;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.oracle.oa.biz.MessageBiz;
import com.oracle.oa.biz.MessageToUserBiz;
import com.oracle.oa.biz.ReadCommonMessageBiz;
import com.oracle.oa.biz.UserInfoBiz;
import com.oracle.oa.biz.impl.OperatelogBizImpl;
import com.oracle.oa.entity.Message;
import com.oracle.oa.entity.Messagetouser;
import com.oracle.oa.entity.Readcommonmessage;
import com.oracle.oa.entity.Userinfo;

public class MessageAction extends ActionSupport implements SessionAware {

	Map<String, Object> session;
	private Message m;
	private String type;
	private String myresult;
	private Messagetouser mtu;
	private List ck;
	private List touser;
	private String usname;

	UserInfoBiz ubiz;
	MessageBiz mbiz;
	MessageToUserBiz mtubiz;
	ReadCommonMessageBiz rcmbiz;
	
	/**
	 * 发布时先跳到发布页面
	 * @return
	 */
	public String one(){
		Message amessage = null;
		session.put("amessage", amessage);
		return "pubmess";		
	}
	
	/**
	 * 跳到收件箱
	 * @return
	 */
	public String inbox(){
		int type=2;
		String newpage="pg";
		session.put("type", type);
		session.put("newpage", newpage);
		return SUCCESS;
	}
	
	/**
	 * 跳到草稿箱
	 * @return
	 */
	public String draftbox(){
		int type=3;
		String newpage="pg";
		session.put("type", type);
		session.put("newpage", newpage);
		return SUCCESS;
	}
	
	/**
	 * 跳到发件箱
	 * @return
	 */
	public String outbox(){
		int type=4;
		String newpage="pg";
		session.put("type", type);
		session.put("newpage", newpage);
		return SUCCESS;
	}
	
	public String mess(){
		int type=10;
		session.put("type", type);
		return SUCCESS;
	}
	
	public String getamessage(){
		Message message = (Message) mbiz.getById(Message.class, m.getMessageid());
		session.put("amessage", message);
		if(type!=null){
			if("two".equals(type)){
				//更新前跳转
				myresult="/admin_cg/hzx/pubmessage.jsp";
			}else if("show".equals(type)){
				//显示消息内容
				myresult="/admin_cg/hzx/messageContent.jsp";
			}else if("showtouser".equals(type)){
				//显示消息收件人
				myresult="/admin_cg/hzx/messagetouser.jsp";
			}else if("showall".equals(type)){
				//显示消息详情
				myresult="/admin_cg/hzx/messageDetails.jsp";
			}else if("showallandup".equals(type)){
				//收件箱中点击弹框显示全部内容，并将未读消息改为已读消息,并在消息已读表中插入已读用户消息
				Messagetouser messagetu = (Messagetouser) mtubiz.getById(Messagetouser.class, mtu.getId());
				long ir = messagetu.getIfread();
				if(ir==0){
					messagetu.setIfread(1);
					mtubiz.update(messagetu);
					Userinfo u = (Userinfo) session.get("userinfo");
					Message mm = (Message) mbiz.getById(Message.class, m.getMessageid());
					Readcommonmessage rcm = new Readcommonmessage(u, mm);
					rcmbiz.save(rcm);
					OperatelogBizImpl.add("修改",messagetu.getMessage().getMessageid()+"消息"+messagetu.getUserinfo().getUserid()+"已查看");
					OperatelogBizImpl.add("增加", "增加一条用户已读消息"+rcm.getReadid());
					//查询消息发送对象表所有内容mesagetouser
					List<Messagetouser> messagetouserList=mtubiz.getAll(Messagetouser.class);
					session.put("messagetouserList", messagetouserList);
				}
				int type=2;
				session.put("type", type);
				myresult="/admin_cg/hzx/messageDetails.jsp";
			}
		}
		
		return SUCCESS;
	}
	
	/**
	 * 消息发布
	 * @return
	 */
	public String fabu(){
		Message amessage = (Message) mbiz.getById(Message.class, m.getMessageid());
		amessage.setIfpublish(1);
		mbiz.update(amessage);
		String up="up";
		session.put("up", up);
		OperatelogBizImpl.add("修改", "发布消息id为"+m.getMessageid()+"的消息");
		return SUCCESS;
	}
	
	/**
	 * 批量删除message
	 * @return
	 */
	public String delete(){
		//若消息发送对象表中含有message,批量删除
		List listmtu= mtubiz.getByProperty(Messagetouser.class, "message.messageid", m.getMessageid());
		if(listmtu!=null && listmtu.size()>0){
			mtubiz.deleteBymessageid(m.getMessageid());
			OperatelogBizImpl.add("删除", "批量删除消息发送表中messageid为"+m.getMessageid()+"的数据");
		}
		//若用户已读消息表中含有message,批量删除
		List listrcm= rcmbiz.getByProperty(Readcommonmessage.class, "message.messageid", m.getMessageid());
		if(listrcm!=null && listrcm.size()>0){
			rcmbiz.deleteBymessageid(m.getMessageid());
			OperatelogBizImpl.add("删除", "批量删除用户已读表中messageid为"+m.getMessageid()+"的数据");
		}
		Message mm = (Message) mbiz.getById(Message.class, m.getMessageid());
		mbiz.delete(mm);
		String up="up";
		session.put("up", up);
		OperatelogBizImpl.add("删除", "删除消息id为"+m.getMessageid()+"的消息");
		return SUCCESS;
	}
	
	/**
	 * 更新message
	 * @return
	 */
	public String update(){
		mbiz.update(m);
		String up="up";
		session.put("up", up);
		OperatelogBizImpl.add("修改", "修改消息id为"+m.getMessageid()+"的消息");
		return SUCCESS;
	}
	
	/**
	 * 添加一个新的message
	 * @return
	 */
	public String add(){
		Userinfo u = (Userinfo) session.get("userinfo");
		Date date = new Date();
		m.setUserinfo(u);
		m.setIfpublish(0);
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		m.setRecordtime(sm.format(date));
		mbiz.save(m);
		OperatelogBizImpl.add("增加", "增加一条消息");
		if(touser!=null &&touser.size()>0){
			//判断选中了谁，消息发送的对象
			for(int i=0;i<touser.size();i++){
				long uid = Long.parseLong(touser.get(i).toString());				
				Userinfo ui = (Userinfo) ubiz.getById(Userinfo.class, uid);
				Messagetouser mtu = new Messagetouser(ui, m, 0);
				//消息发送对象插入
				mtubiz.save(mtu);
				OperatelogBizImpl.add("增加", "插入一条消息发送对象"+mtu.getId());
			}
		}else{
			//如果没有用list，则发送对象为所有人
			List<Userinfo> ulist = (List<Userinfo>) session.get("userinfoList");
			for(int i=0;i<ulist.size();i++){
				Userinfo ui = ulist.get(i);
				if(ui.getUserid()!=u.getUserid()){
					Messagetouser mtu = new Messagetouser(ui, m, 0);
					//消息发送对象插入
					mtubiz.save(mtu);
					OperatelogBizImpl.add("增加", "插入一条消息发送对象"+mtu.getId());
				}
			}
		}
		String up="up";
		session.put("up", up);
		return SUCCESS;
	}
	
	/**
	 * 删除选定项
	 * @return
	 */
	public String deletesome(){
		if(ck!=null && ck.size()>0){
			//得到要删除的list集合 ck,进行循环判断
			for(int i=0;i<ck.size();i++){
				//得到第i条的messageid
				long mid=Long.parseLong(ck.get(i).toString());
				//得到发送对象表中message，messageid为mid的数据集合
				List listmtu= mtubiz.getByProperty(Messagetouser.class, "message.messageid", mid);
				if(listmtu!=null && listmtu.size()>0){
					//如果集合存在，批量删除这些数据
					mtubiz.deleteBymessageid(mid);
					OperatelogBizImpl.add("删除", "批量删除消息发送表中messageid为"+mid+"的数据");
				}
				//得到用户已读消息表中message，messageid为mid的数据集合
				List listrcm= rcmbiz.getByProperty(Readcommonmessage.class, "message.messageid", mid);
				if(listrcm!=null && listrcm.size()>0){
					//如果集合存在，批量删除这些数据
					rcmbiz.deleteBymessageid(mid);
					OperatelogBizImpl.add("删除", "批量删除用户已读表中messageid为"+mid+"的数据");
				}
				Message m = (Message) mbiz.getById(Message.class, mid);
				mbiz.delete(m);
				OperatelogBizImpl.add("删除", "删除选定的message消息"+m.getMessageid());
			}
			List<Message> messageList = mbiz.getAll(Message.class);
			session.put("messageList", messageList);
			if("draftbox".equals(type)){
				int type=3;
				session.put("type", type);
			}else if("outbox".equals(type)){
				int type=4;
				session.put("type", type);
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 姓名自动补全
	 * @return
	 * @throws Exception
	 */
	public String byname() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		String uname = request.getParameter("usname");
		String un=new String(uname.getBytes("ISO-8859-1"),"UTF-8");
		ActionContext context=ActionContext.getContext();
		HttpServletResponse response=(HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);
		PrintWriter out=response.getWriter();
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset='UTF-8'");
		String unames="";	
		List<Userinfo> unamelist = ubiz.getByPorUsername(un);
		System.out.println(un);
		Userinfo user = (Userinfo) session.get("userinfo");
		if(unamelist!=null && unamelist.size()>0){
			for(int i=0;i<unamelist.size();i++){
				Userinfo u = unamelist.get(i);
				if(u.getUserid()!=user.getUserid()){
					String str=u.getUsername();
					unames=unames+"<span onclick=\"qu('"+str+"')\">"+str+"</span><br/>";
					System.out.println(unames);
				}
			}
		}
		out.print(unames);
		out.flush();
		out.close();
		return "pubmess";
	}
	
	public void setSession(Map<String, Object> arg0) {

		this.session=arg0;
	}	

	public Message getM() {
		return m;
	}

	public void setM(Message m) {
		this.m = m;
	}


	public String getType() {
		return type;
	}


	public void setType(String type) {
		this.type = type;
	}
	
	public String getMyresult() {
		return myresult;
	}


	public void setMyresult(String myresult) {
		this.myresult = myresult;
	}

	public Messagetouser getMtu() {
		return mtu;
	}

	public void setMtu(Messagetouser mtu) {
		this.mtu = mtu;
	}

	public MessageBiz getMbiz() {
		return mbiz;
	}

	public void setMbiz(MessageBiz mbiz) {
		this.mbiz = mbiz;
	}
	

	public MessageToUserBiz getMtubiz() {
		return mtubiz;
	}

	public void setMtubiz(MessageToUserBiz mtubiz) {
		this.mtubiz = mtubiz;
	}

	public ReadCommonMessageBiz getRcmbiz() {
		return rcmbiz;
	}

	public void setRcmbiz(ReadCommonMessageBiz rcmbiz) {
		this.rcmbiz = rcmbiz;
	}

	public List getCk() {
		return ck;
	}

	public void setCk(List ck) {
		this.ck = ck;
	}

	public List getTouser() {
		return touser;
	}

	public void setTouser(List touser) {
		this.touser = touser;
	}

	public UserInfoBiz getUbiz() {
		return ubiz;
	}

	public void setUbiz(UserInfoBiz ubiz) {
		this.ubiz = ubiz;
	}


	public String getUsname() {
		return usname;
	}


	public void setUsname(String usname) {
		this.usname = usname;
	}


	
	
	
}
